/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author snowangelic
 */
public class ReplaySaver {
    long timeStampOffset=0;
    long pauseTriggerOffset=0;
    int gameState=Model.EnigmaModel.GAME_NOT_STARTED;
    FileWriter replayFile;

    public ReplaySaver(){
        try {
            replayFile = new FileWriter("./replay.rpl");
        } catch (IOException ex) {
            Logger.getLogger(ReplaySaver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void activeItemUsed() {
        if(gameState==1){
            try {
                replayFile.append((System.currentTimeMillis() - timeStampOffset) + "|aiu"+"\n");
            } catch (IOException ex) {
            }
        }
    }

    void inventoryScrolled() {
        if(gameState==1){
            try {
                replayFile.append((System.currentTimeMillis() - timeStampOffset) + "|is"+"\n");
            } catch (IOException ex) {
            }
        }
    }

    void mouseMoved(int[] coords){
        if(gameState==1){
            try {
                replayFile.append((System.currentTimeMillis() - timeStampOffset) + "|mm|"+coords[0]+"|"+coords[1]+"\n");
            } catch (IOException ex) {
            }
        }
    }

    void saveReplay(File file) {
        try {
            replayFile.close();
        } catch (IOException ex) {
            Logger.getLogger(ReplaySaver.class.getName()).log(Level.SEVERE, null, ex);
        }
        EnigmaUtils.FileCopier.copyFile(new File("./replay.rpl"), file);
    }
    void setGameState(int newGameState){
        if(newGameState==Model.EnigmaModel.GAME_PLAYING)
            timeStampOffset+=System.currentTimeMillis()-pauseTriggerOffset;
        else if(newGameState==Model.EnigmaModel.GAME_PAUSED)
            pauseTriggerOffset=System.currentTimeMillis();
        gameState=newGameState;
    }

    void setLevelFile(File level) throws IOException {
        replayFile.append(level.getAbsolutePath()+"\n");
    }
}
